package com.chuhe.lottery.dao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuhe.lottery.domain.entity.Winners;
import com.chuhe.lottery.domain.entity.dto.WinnerDto;
import com.chuhe.lottery.mapper.WinnersMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张楚河
 * @since 2025-06-03
 */
@Service
public class WinnersDao extends ServiceImpl<WinnersMapper, Winners> {
    /**
     * 根据id查询用户
     * @Author: 楚河
     * @Date:  17:23
     */
    public Winners selectUserById(int userId) {
        return lambdaQuery()
                .eq(Winners::getUserId, userId)
                .one();
    }
    /**
     * 查询获奖人数总和
     * @Author: 楚河
     * @Date: 2025-06-04 8:35
     */
    public Integer selectAllCount() {
        return lambdaQuery()
                .count();
    }

    /**
     * 查询获奖详细信息
     * @Author: 楚河
     * @Date: 2025-06-04 8:52
     */
    public WinnerDto selectWinnerInfo() {
        // 所有奖项
        List<Winners> list = lambdaQuery().list();

        // 过滤具体奖项
        Map<String, Long> prizeCountMap = list().stream()
                .collect(Collectors.groupingBy(Winners::getAwards, Collectors.counting()));

        //结果封装
        return WinnerDto.builder()
                .firstPrizeCount(prizeCountMap.getOrDefault("一等奖", 0L).intValue())
                .secondPrizeCount(prizeCountMap.getOrDefault("二等奖", 0L).intValue())
                .thirdPrizeCount(prizeCountMap.getOrDefault("三等奖", 0L).intValue())
                .build();

    }
}
